# ==============================================================================
#
# utils.pm
#
# Account maint utility functions
#
# $Id: utils.pm,v 1.7 2005/10/11 17:50:39 dyoung2 Exp $
#
# Darren Young [darren.young@gsb.uchicago.edu]
#
# ==============================================================================
#
# ChangeLog
#
# $Log: utils.pm,v $
# Revision 1.7  2005/10/11 17:50:39  dyoung2
#   * Added _makeRandomSalt().
#   * Changed _makeRandomPassword() to use _makeRandomSalt().
#
# Revision 1.6  2005/08/25 22:38:27  dyoung2
#   * Standardized BEGIN headers.
#
# Revision 1.5  2005/06/21 17:08:58  dyoung2
#   * Added docs for print_* functions.
#   * Changed return codes to use error constants.
#
# Revision 1.4  2005/06/14 17:52:47  dyoung2
#   * Added debug() statements to _makeRandomPassword
#
# Revision 1.3  2005/06/14 17:23:38  dyoung2
#   * Added _makeRandomPassword().
#
# ==============================================================================

my  $cvsid   = '$Id: utils.pm,v 1.7 2005/10/11 17:50:39 dyoung2 Exp $';
my  @cvsinfo = split( ' ', $cvsid );
our $NAME    = File::Basename::basename($0);
our $VERSION = $cvsinfo[2];

# ------------------------------------------------------------------------------
#                             B E G I N
# ------------------------------------------------------------------------------
BEGIN {

    # Pragmas
    use strict;

    # "Standard" modules we use
    use FindBin;
    use Config::Simple;
    use lib "$FindBin::Bin/../lib";

    # Standard account maint modules to use
    use logging;
    use errcodes;
    use utils;
    
    # Modules that this module uses
    use Net::LDAP;

}


# ------------------------------------------------------------------------------ 
#                             V A R I A B L E S
# ------------------------------------------------------------------------------ 
our $LOGFILE    = "$FindBin::Bin/../log/acctmgr.log" unless($LOGFILE); 
our $CONFIGFILE = "$FindBin::Bin/../etc/acctmgr.cfg" unless($CONFIGFILE); 


# ------------------------------------------------------------------------------
#                             C O N F I G   F I L E
# ------------------------------------------------------------------------------
our $CFG = new Config::Simple( filename => $CONFIGFILE ) or die "$!\n" unless($CFG);


# ==============================================================================
# NAME        : print_version
# DESCRIPTION : Print the version of a module
# ARGUMENTS   : none
# RETURN      : none
# NOTES       : Requires global (our) variable VERSION
# ==============================================================================
sub print_version {
    print "$NAME version $VERSION\n";
    exit(SHELL_TRUE);
}


# ==============================================================================
# NAME        : print_usage
# DESCRIPTION : Prints the usage of a module
# ARGUMENTS   : none
# RETURN      : none
# NOTES       : Requires global (our) variable USAGE
# ==============================================================================
sub print_usage {
    print "$USAGE";
    exit(SHELL_TRUE);
}


# ==============================================================================
# NAME        : fixme
# DESCRIPTION : Print a formatted message to fix something at level debug
# ARGUMENTS   : required(string(message))
# RETURN      : TRUE
# NOTES       : none
# ==============================================================================
sub fixme { 
    my ($message) = @_; 
    my ( $package, $filename, $line ) = caller;  
    debug("** FIXME ** : $message (pkg->$package, file->$filename, line->$line)"); 
    return(TRUE); 
} 


# ==============================================================================
# NAME        : _makeRandomPassword
# DESCRIPTION : generate a random crypted password
# ARGUMENTS   : none
# RETURN      : string(password)
# NOTES       : none
# ==============================================================================
sub _makeRandomPassword {

    my $name = "_makeRandomPassword";
    my ( $package, $filename, $line ) = caller;
    debug("$name: entering with args @_");
    debug("$name: called from package->$package, filename->$filename, line->$line");

    my $password_length = 8;
    my $password;
    my $_rand;
    my @chars;
    my $salt;
    my $retval;

    @chars = split(" ",
                     "a b c d e f g h i j k l m n o p q r s t
                      u v w x y z - _ % # | 0 1 2 3 4 5 6 7 8 9"
                  );

    $salt = _makeRandomSalt();

    srand;

    for (my $i=0; $i <= $password_length ;$i++) {
        $_rand = int(rand 41);
        $password .= $chars[$_rand];
    }

    my $retval = crypt($password, $salt);
    debug("$name: returning with val $retval");
    return($retval);
}


# ==============================================================================
# NAME        : _makeRandomSalt
# DESCRIPTION : generate a random salt for crypt()ing
# ARGUMENTS   : none
# RETURN      : string(salt)
# NOTES       : none
# ==============================================================================
sub _makeRandomSalt {

    my $name = "_makeRandomSalt";
    my ( $package, $filename, $line ) = caller;
    debug("$name: entering with args @_");
    debug("$name: called from package->$package, filename->$filename, line->$line");

    my $salt;
    my $retval;

    srand;
    $salt = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64];
    $retval = $salt;

    debug("$name: returning with val $retval");
    return($retval);
}


1;
